/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.hotel.test.service.persistence;

import com.hotel.test.NoSuchRomTypeException;
import com.hotel.test.model.RomType;
import com.hotel.test.model.impl.RomTypeImpl;
import com.hotel.test.model.impl.RomTypeModelImpl;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.RowMapper;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the rom type service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Vien-IT
 * @see RomTypePersistence
 * @see RomTypeUtil
 * @generated
 */
public class RomTypePersistenceImpl extends BasePersistenceImpl<RomType>
	implements RomTypePersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link RomTypeUtil} to access the rom type persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = RomTypeImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ROOMNAME = new FinderPath(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeModelImpl.FINDER_CACHE_ENABLED, RomTypeImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByRoomName",
			new String[] {
				String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROOMNAME =
		new FinderPath(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeModelImpl.FINDER_CACHE_ENABLED, RomTypeImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByRoomName",
			new String[] { String.class.getName() },
			RomTypeModelImpl.ROOMNAME_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_ROOMNAME = new FinderPath(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByRoomName",
			new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeModelImpl.FINDER_CACHE_ENABLED, RomTypeImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeModelImpl.FINDER_CACHE_ENABLED, RomTypeImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);

	/**
	 * Caches the rom type in the entity cache if it is enabled.
	 *
	 * @param romType the rom type
	 */
	public void cacheResult(RomType romType) {
		EntityCacheUtil.putResult(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeImpl.class, romType.getPrimaryKey(), romType);

		romType.resetOriginalValues();
	}

	/**
	 * Caches the rom types in the entity cache if it is enabled.
	 *
	 * @param romTypes the rom types
	 */
	public void cacheResult(List<RomType> romTypes) {
		for (RomType romType : romTypes) {
			if (EntityCacheUtil.getResult(
						RomTypeModelImpl.ENTITY_CACHE_ENABLED,
						RomTypeImpl.class, romType.getPrimaryKey()) == null) {
				cacheResult(romType);
			}
			else {
				romType.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all rom types.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(RomTypeImpl.class.getName());
		}

		EntityCacheUtil.clearCache(RomTypeImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the rom type.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(RomType romType) {
		EntityCacheUtil.removeResult(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeImpl.class, romType.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@Override
	public void clearCache(List<RomType> romTypes) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (RomType romType : romTypes) {
			EntityCacheUtil.removeResult(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
				RomTypeImpl.class, romType.getPrimaryKey());
		}
	}

	/**
	 * Creates a new rom type with the primary key. Does not add the rom type to the database.
	 *
	 * @param romId the primary key for the new rom type
	 * @return the new rom type
	 */
	public RomType create(long romId) {
		RomType romType = new RomTypeImpl();

		romType.setNew(true);
		romType.setPrimaryKey(romId);

		return romType;
	}

	/**
	 * Removes the rom type with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param romId the primary key of the rom type
	 * @return the rom type that was removed
	 * @throws com.hotel.test.NoSuchRomTypeException if a rom type with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public RomType remove(long romId)
		throws NoSuchRomTypeException, SystemException {
		return remove(Long.valueOf(romId));
	}

	/**
	 * Removes the rom type with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the rom type
	 * @return the rom type that was removed
	 * @throws com.hotel.test.NoSuchRomTypeException if a rom type with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public RomType remove(Serializable primaryKey)
		throws NoSuchRomTypeException, SystemException {
		Session session = null;

		try {
			session = openSession();

			RomType romType = (RomType)session.get(RomTypeImpl.class, primaryKey);

			if (romType == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchRomTypeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(romType);
		}
		catch (NoSuchRomTypeException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected RomType removeImpl(RomType romType) throws SystemException {
		romType = toUnwrappedModel(romType);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, romType);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		clearCache(romType);

		return romType;
	}

	@Override
	public RomType updateImpl(com.hotel.test.model.RomType romType,
		boolean merge) throws SystemException {
		romType = toUnwrappedModel(romType);

		boolean isNew = romType.isNew();

		RomTypeModelImpl romTypeModelImpl = (RomTypeModelImpl)romType;

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, romType, merge);

			romType.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew || !RomTypeModelImpl.COLUMN_BITMASK_ENABLED) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		else {
			if ((romTypeModelImpl.getColumnBitmask() &
					FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROOMNAME.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						romTypeModelImpl.getOriginalRoomName()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROOMNAME, args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROOMNAME,
					args);

				args = new Object[] { romTypeModelImpl.getRoomName() };

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROOMNAME, args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROOMNAME,
					args);
			}
		}

		EntityCacheUtil.putResult(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
			RomTypeImpl.class, romType.getPrimaryKey(), romType);

		return romType;
	}

	protected RomType toUnwrappedModel(RomType romType) {
		if (romType instanceof RomTypeImpl) {
			return romType;
		}

		RomTypeImpl romTypeImpl = new RomTypeImpl();

		romTypeImpl.setNew(romType.isNew());
		romTypeImpl.setPrimaryKey(romType.getPrimaryKey());

		romTypeImpl.setRomId(romType.getRomId());
		romTypeImpl.setUserId(romType.getUserId());
		romTypeImpl.setUserName(romType.getUserName());
		romTypeImpl.setCreateDate(romType.getCreateDate());
		romTypeImpl.setModifiedDate(romType.getModifiedDate());
		romTypeImpl.setRoomName(romType.getRoomName());
		romTypeImpl.setInformation(romType.getInformation());
		romTypeImpl.setNumberOfGuest(romType.getNumberOfGuest());
		romTypeImpl.setRice(romType.getRice());
		romTypeImpl.setNumberOfRoom(romType.getNumberOfRoom());
		romTypeImpl.setHotelId(romType.getHotelId());

		return romTypeImpl;
	}

	/**
	 * Returns the rom type with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the rom type
	 * @return the rom type
	 * @throws com.liferay.portal.NoSuchModelException if a rom type with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public RomType findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the rom type with the primary key or throws a {@link com.hotel.test.NoSuchRomTypeException} if it could not be found.
	 *
	 * @param romId the primary key of the rom type
	 * @return the rom type
	 * @throws com.hotel.test.NoSuchRomTypeException if a rom type with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public RomType findByPrimaryKey(long romId)
		throws NoSuchRomTypeException, SystemException {
		RomType romType = fetchByPrimaryKey(romId);

		if (romType == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + romId);
			}

			throw new NoSuchRomTypeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				romId);
		}

		return romType;
	}

	/**
	 * Returns the rom type with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the rom type
	 * @return the rom type, or <code>null</code> if a rom type with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public RomType fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Returns the rom type with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param romId the primary key of the rom type
	 * @return the rom type, or <code>null</code> if a rom type with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public RomType fetchByPrimaryKey(long romId) throws SystemException {
		RomType romType = (RomType)EntityCacheUtil.getResult(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
				RomTypeImpl.class, romId);

		if (romType == _nullRomType) {
			return null;
		}

		if (romType == null) {
			Session session = null;

			boolean hasException = false;

			try {
				session = openSession();

				romType = (RomType)session.get(RomTypeImpl.class,
						Long.valueOf(romId));
			}
			catch (Exception e) {
				hasException = true;

				throw processException(e);
			}
			finally {
				if (romType != null) {
					cacheResult(romType);
				}
				else if (!hasException) {
					EntityCacheUtil.putResult(RomTypeModelImpl.ENTITY_CACHE_ENABLED,
						RomTypeImpl.class, romId, _nullRomType);
				}

				closeSession(session);
			}
		}

		return romType;
	}

	/**
	 * Returns all the rom types where roomName = &#63;.
	 *
	 * @param roomName the room name
	 * @return the matching rom types
	 * @throws SystemException if a system exception occurred
	 */
	public List<RomType> findByRoomName(String roomName)
		throws SystemException {
		return findByRoomName(roomName, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
			null);
	}

	/**
	 * Returns a range of all the rom types where roomName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param roomName the room name
	 * @param start the lower bound of the range of rom types
	 * @param end the upper bound of the range of rom types (not inclusive)
	 * @return the range of matching rom types
	 * @throws SystemException if a system exception occurred
	 */
	public List<RomType> findByRoomName(String roomName, int start, int end)
		throws SystemException {
		return findByRoomName(roomName, start, end, null);
	}

	/**
	 * Returns an ordered range of all the rom types where roomName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param roomName the room name
	 * @param start the lower bound of the range of rom types
	 * @param end the upper bound of the range of rom types (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of matching rom types
	 * @throws SystemException if a system exception occurred
	 */
	public List<RomType> findByRoomName(String roomName, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROOMNAME;
			finderArgs = new Object[] { roomName };
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ROOMNAME;
			finderArgs = new Object[] { roomName, start, end, orderByComparator };
		}

		List<RomType> list = (List<RomType>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_ROMTYPE_WHERE);

			if (roomName == null) {
				query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_1);
			}
			else {
				if (roomName.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_3);
				}
				else {
					query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_2);
				}
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(RomTypeModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (roomName != null) {
					qPos.add(roomName);
				}

				list = (List<RomType>)QueryUtil.list(q, getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Returns the first rom type in the ordered set where roomName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param roomName the room name
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching rom type
	 * @throws com.hotel.test.NoSuchRomTypeException if a matching rom type could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public RomType findByRoomName_First(String roomName,
		OrderByComparator orderByComparator)
		throws NoSuchRomTypeException, SystemException {
		List<RomType> list = findByRoomName(roomName, 0, 1, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("roomName=");
			msg.append(roomName);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchRomTypeException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Returns the last rom type in the ordered set where roomName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param roomName the room name
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching rom type
	 * @throws com.hotel.test.NoSuchRomTypeException if a matching rom type could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public RomType findByRoomName_Last(String roomName,
		OrderByComparator orderByComparator)
		throws NoSuchRomTypeException, SystemException {
		int count = countByRoomName(roomName);

		List<RomType> list = findByRoomName(roomName, count - 1, count,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("roomName=");
			msg.append(roomName);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchRomTypeException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Returns the rom types before and after the current rom type in the ordered set where roomName = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param romId the primary key of the current rom type
	 * @param roomName the room name
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the previous, current, and next rom type
	 * @throws com.hotel.test.NoSuchRomTypeException if a rom type with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public RomType[] findByRoomName_PrevAndNext(long romId, String roomName,
		OrderByComparator orderByComparator)
		throws NoSuchRomTypeException, SystemException {
		RomType romType = findByPrimaryKey(romId);

		Session session = null;

		try {
			session = openSession();

			RomType[] array = new RomTypeImpl[3];

			array[0] = getByRoomName_PrevAndNext(session, romType, roomName,
					orderByComparator, true);

			array[1] = romType;

			array[2] = getByRoomName_PrevAndNext(session, romType, roomName,
					orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected RomType getByRoomName_PrevAndNext(Session session,
		RomType romType, String roomName, OrderByComparator orderByComparator,
		boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_ROMTYPE_WHERE);

		if (roomName == null) {
			query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_1);
		}
		else {
			if (roomName.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_3);
			}
			else {
				query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

			if (orderByConditionFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByConditionFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByConditionFields[i]);

				if ((i + 1) < orderByConditionFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			String[] orderByFields = orderByComparator.getOrderByFields();

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(RomTypeModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		if (roomName != null) {
			qPos.add(roomName);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByConditionValues(romType);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<RomType> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Returns all the rom types.
	 *
	 * @return the rom types
	 * @throws SystemException if a system exception occurred
	 */
	public List<RomType> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the rom types.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of rom types
	 * @param end the upper bound of the range of rom types (not inclusive)
	 * @return the range of rom types
	 * @throws SystemException if a system exception occurred
	 */
	public List<RomType> findAll(int start, int end) throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the rom types.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of rom types
	 * @param end the upper bound of the range of rom types (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of rom types
	 * @throws SystemException if a system exception occurred
	 */
	public List<RomType> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		FinderPath finderPath = null;
		Object[] finderArgs = new Object[] { start, end, orderByComparator };

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<RomType> list = (List<RomType>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_ROMTYPE);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_ROMTYPE.concat(RomTypeModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<RomType>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<RomType>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(finderPath, finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(finderPath, finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the rom types where roomName = &#63; from the database.
	 *
	 * @param roomName the room name
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByRoomName(String roomName) throws SystemException {
		for (RomType romType : findByRoomName(roomName)) {
			remove(romType);
		}
	}

	/**
	 * Removes all the rom types from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (RomType romType : findAll()) {
			remove(romType);
		}
	}

	/**
	 * Returns the number of rom types where roomName = &#63;.
	 *
	 * @param roomName the room name
	 * @return the number of matching rom types
	 * @throws SystemException if a system exception occurred
	 */
	public int countByRoomName(String roomName) throws SystemException {
		Object[] finderArgs = new Object[] { roomName };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_ROOMNAME,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_ROMTYPE_WHERE);

			if (roomName == null) {
				query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_1);
			}
			else {
				if (roomName.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_3);
				}
				else {
					query.append(_FINDER_COLUMN_ROOMNAME_ROOMNAME_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (roomName != null) {
					qPos.add(roomName);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_ROOMNAME,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns the number of rom types.
	 *
	 * @return the number of rom types
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_ROMTYPE);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Returns all the bookings associated with the rom type.
	 *
	 * @param pk the primary key of the rom type
	 * @return the bookings associated with the rom type
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.hotel.test.model.Booking> getBookings(long pk)
		throws SystemException {
		return getBookings(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
	}

	/**
	 * Returns a range of all the bookings associated with the rom type.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the rom type
	 * @param start the lower bound of the range of rom types
	 * @param end the upper bound of the range of rom types (not inclusive)
	 * @return the range of bookings associated with the rom type
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.hotel.test.model.Booking> getBookings(long pk, int start,
		int end) throws SystemException {
		return getBookings(pk, start, end, null);
	}

	public static final FinderPath FINDER_PATH_GET_BOOKINGS = new FinderPath(com.hotel.test.model.impl.BookingModelImpl.ENTITY_CACHE_ENABLED,
			com.hotel.test.model.impl.BookingModelImpl.FINDER_CACHE_ENABLED,
			com.hotel.test.model.impl.BookingImpl.class,
			com.hotel.test.service.persistence.BookingPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getBookings",
			new String[] {
				Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});

	/**
	 * Returns an ordered range of all the bookings associated with the rom type.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param pk the primary key of the rom type
	 * @param start the lower bound of the range of rom types
	 * @param end the upper bound of the range of rom types (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of bookings associated with the rom type
	 * @throws SystemException if a system exception occurred
	 */
	public List<com.hotel.test.model.Booking> getBookings(long pk, int start,
		int end, OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };

		List<com.hotel.test.model.Booking> list = (List<com.hotel.test.model.Booking>)FinderCacheUtil.getResult(FINDER_PATH_GET_BOOKINGS,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				String sql = null;

				if (orderByComparator != null) {
					sql = _SQL_GETBOOKINGS.concat(ORDER_BY_CLAUSE)
										  .concat(orderByComparator.getOrderBy());
				}
				else {
					sql = _SQL_GETBOOKINGS.concat(com.hotel.test.model.impl.BookingModelImpl.ORDER_BY_SQL);
				}

				SQLQuery q = session.createSQLQuery(sql);

				q.addEntity("HT_Booking",
					com.hotel.test.model.impl.BookingImpl.class);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				list = (List<com.hotel.test.model.Booking>)QueryUtil.list(q,
						getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_GET_BOOKINGS,
						finderArgs);
				}
				else {
					bookingPersistence.cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_GET_BOOKINGS,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	public static final FinderPath FINDER_PATH_GET_BOOKINGS_SIZE = new FinderPath(com.hotel.test.model.impl.BookingModelImpl.ENTITY_CACHE_ENABLED,
			com.hotel.test.model.impl.BookingModelImpl.FINDER_CACHE_ENABLED,
			com.hotel.test.model.impl.BookingImpl.class,
			com.hotel.test.service.persistence.BookingPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"getBookingsSize", new String[] { Long.class.getName() });

	/**
	 * Returns the number of bookings associated with the rom type.
	 *
	 * @param pk the primary key of the rom type
	 * @return the number of bookings associated with the rom type
	 * @throws SystemException if a system exception occurred
	 */
	public int getBookingsSize(long pk) throws SystemException {
		Object[] finderArgs = new Object[] { pk };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_BOOKINGS_SIZE,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				SQLQuery q = session.createSQLQuery(_SQL_GETBOOKINGSSIZE);

				q.addScalar(COUNT_COLUMN_NAME,
					com.liferay.portal.kernel.dao.orm.Type.LONG);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(pk);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_GET_BOOKINGS_SIZE,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	public static final FinderPath FINDER_PATH_CONTAINS_BOOKING = new FinderPath(com.hotel.test.model.impl.BookingModelImpl.ENTITY_CACHE_ENABLED,
			com.hotel.test.model.impl.BookingModelImpl.FINDER_CACHE_ENABLED,
			com.hotel.test.model.impl.BookingImpl.class,
			com.hotel.test.service.persistence.BookingPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"containsBooking",
			new String[] { Long.class.getName(), Long.class.getName() });

	/**
	 * Returns <code>true</code> if the booking is associated with the rom type.
	 *
	 * @param pk the primary key of the rom type
	 * @param bookingPK the primary key of the booking
	 * @return <code>true</code> if the booking is associated with the rom type; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsBooking(long pk, long bookingPK)
		throws SystemException {
		Object[] finderArgs = new Object[] { pk, bookingPK };

		Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_BOOKING,
				finderArgs, this);

		if (value == null) {
			try {
				value = Boolean.valueOf(containsBooking.contains(pk, bookingPK));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (value == null) {
					value = Boolean.FALSE;
				}

				FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_BOOKING,
					finderArgs, value);
			}
		}

		return value.booleanValue();
	}

	/**
	 * Returns <code>true</code> if the rom type has any bookings associated with it.
	 *
	 * @param pk the primary key of the rom type to check for associations with bookings
	 * @return <code>true</code> if the rom type has any bookings associated with it; <code>false</code> otherwise
	 * @throws SystemException if a system exception occurred
	 */
	public boolean containsBookings(long pk) throws SystemException {
		if (getBookingsSize(pk) > 0) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * Initializes the rom type persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.hotel.test.model.RomType")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<RomType>> listenersList = new ArrayList<ModelListener<RomType>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<RomType>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}

		containsBooking = new ContainsBooking();
	}

	public void destroy() {
		EntityCacheUtil.removeCache(RomTypeImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@BeanReference(type = BookingPersistence.class)
	protected BookingPersistence bookingPersistence;
	@BeanReference(type = HotelPersistence.class)
	protected HotelPersistence hotelPersistence;
	@BeanReference(type = LocalPersistence.class)
	protected LocalPersistence localPersistence;
	@BeanReference(type = RomTypePersistence.class)
	protected RomTypePersistence romTypePersistence;
	@BeanReference(type = TblCommentPersistence.class)
	protected TblCommentPersistence tblCommentPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	protected ContainsBooking containsBooking;

	protected class ContainsBooking {
		protected ContainsBooking() {
			_mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
					_SQL_CONTAINSBOOKING,
					new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
					RowMapper.COUNT);
		}

		protected boolean contains(long romId, long bookId) {
			List<Integer> results = _mappingSqlQuery.execute(new Object[] {
						new Long(romId), new Long(bookId)
					});

			if (results.size() > 0) {
				Integer count = results.get(0);

				if (count.intValue() > 0) {
					return true;
				}
			}

			return false;
		}

		private MappingSqlQuery<Integer> _mappingSqlQuery;
	}

	private static final String _SQL_SELECT_ROMTYPE = "SELECT romType FROM RomType romType";
	private static final String _SQL_SELECT_ROMTYPE_WHERE = "SELECT romType FROM RomType romType WHERE ";
	private static final String _SQL_COUNT_ROMTYPE = "SELECT COUNT(romType) FROM RomType romType";
	private static final String _SQL_COUNT_ROMTYPE_WHERE = "SELECT COUNT(romType) FROM RomType romType WHERE ";
	private static final String _SQL_GETBOOKINGS = "SELECT {HT_Booking.*} FROM HT_Booking INNER JOIN HT_RomType ON (HT_RomType.romId = HT_Booking.romId) WHERE (HT_RomType.romId = ?)";
	private static final String _SQL_GETBOOKINGSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM HT_Booking WHERE romId = ?";
	private static final String _SQL_CONTAINSBOOKING = "SELECT COUNT(*) AS COUNT_VALUE FROM HT_Booking WHERE romId = ? AND bookId = ?";
	private static final String _FINDER_COLUMN_ROOMNAME_ROOMNAME_1 = "romType.roomName IS NULL";
	private static final String _FINDER_COLUMN_ROOMNAME_ROOMNAME_2 = "romType.roomName = ?";
	private static final String _FINDER_COLUMN_ROOMNAME_ROOMNAME_3 = "(romType.roomName IS NULL OR romType.roomName = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "romType.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RomType exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RomType exists with the key {";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(RomTypePersistenceImpl.class);
	private static RomType _nullRomType = new RomTypeImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<RomType> toCacheModel() {
				return _nullRomTypeCacheModel;
			}
		};

	private static CacheModel<RomType> _nullRomTypeCacheModel = new CacheModel<RomType>() {
			public RomType toEntityModel() {
				return _nullRomType;
			}
		};
}